package com.jc.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.SignatureException;

import org.apache.commons.codec.digest.DigestUtils;

import sun.misc.BASE64Encoder;

public class MD5 {

	public static String md5(String source) {
		StringBuffer sb = new StringBuffer(32);
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(source.getBytes("utf-8"));

			for (int i = 0; i < array.length; i++) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
			}
		} catch (Exception e) {
			return null;
		}
		return sb.toString();
	}
	
	public static String passmd5(String source) {
		StringBuffer sb = new StringBuffer(32);
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(source.getBytes("utf-8"));

			for (int i = 0; i < array.length; i++) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
			}
		} catch (Exception e) {
			return null;
		}
		return sb.toString();
	}
	
	
	
	public static String encode(String str){
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            byte []afterEncode = md5.digest(str.getBytes());

            //Base64编码  它的作用是用于将非ascii码转成ascii码
            BASE64Encoder b64 = new BASE64Encoder();
            return b64.encode(afterEncode);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
	
	/** 
	* 功能：支付宝MD5签名处理核心文件，不需要修改
	* 版本：3.3
	* 说明：
	* 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
	* 该代码仅供学习和研究支付宝接口使用，只是提供一个
	* */

    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
    	text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
    
    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param sign 签名结果
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static boolean verify(String text, String sign, String key, String input_charset) {
    	text = text + key;
    	String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
    	if(mysign.equals(sign)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }

    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException 
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }

    public static void main(String[] args) {
		//System.out.println(passmd5("eyJhZGRyZXNzIjogIuS4iua1t+WNlyIsImFyZWEiOiAiIiwiYmFVc2VySWQiOiAwLCJiaWxsSGVhZGVyIjogIuaIkeeahOWPkeelqCIsImJpbGxUeXBlIjogMSwiY2FzaENvaW4iOiAwLCJjaXR5IjogIiIsImRlbGl2ZXJ5V2F5IjogMCwiaXNCaWxsIjogMSwibGlua01hbiI6ICLmr5vms73lsKciLCJtYWxsU3RvcmVJZCI6IDAsIm1vYmlsZSI6ICIxODgyNzA3MDcwMCIsIm9yZGVyRGV0YWlsTGlzdCI6IFt7ImFjdGl2aXR5VHlwZSI6IDAsImFmdGVySXNSZWZ1bmQiOiAwLCJhZnRlclJlYXNvbiI6ICIiLCJhZnRlclNhbGVzSWQiOiAwLCJhZnRlclN0YXR1cyI6IDUsImFmdGVyVHlwZSI6IDAsImFwaUdvb2RzSWQiOiAwLCJhcGlPcmRlckRldGFpbElkIjogMCwiYXBwbHl3ZWxmYXJlSWQiOiAwLCJiYWNrdHJhY2tDYXNoIjogMCwiYnJhbmRJZCI6IDk4LCJicmFuZE5hbWUiOiAi5ZCJ6KGj6LShIiwiY2hhbm5lbCI6IDAsImNvbW1lbnRTdGF0dXMiOiAwLCJkZWxpdmVyeVdheSI6IDEsImdvb2RJZCI6IDc1LCJnb29kTmFtZSI6ICLlkInooaPotKHnlLflo6vplb/oopbooazooasiLCJncm91cEJ1eWluZ0lkIjogMCwiaW52ZW50b3J5SWQiOiA5MSwiaXNBZnRlclNhbGUiOiAwLCJtYWxsU3RvcmVJZCI6IDAsIm5hdmlnYXRlSW1nMSI6ICJpbWFnZXMvZ29vZHNTcGVjSW1nLzIwMTYxMjEyMTEzODE3ODMzNi5qcGciLCJwcm9kdWN0SWQiOiA2OCwicHJvZHVjdFByaWNlIjogMTM4MDAsInNhbGVQcmljZSI6ODg4OCwicXVhbnRpdHkiOiAxLCJzaGFyZUFjdGl2aXR5SWQiOiAwLCJzaGFyZVNlcmlhbElkIjogMSwic3BlY05hbWUiOiAiR0FTMTA5MDE3MzAyIFMiLCJzdG9yZUlkIjogMjAsInRhbGVudElkIjogNTEwLCJ0YWxlbnRTZXJpYWxFZGl0YW5kc2hhcmVJZCI6IDAsInRhbGVudFNlcmlhbElkIjogMCwidGFsZW50U2hhcmVHb29kSWQiOiAwLCJ0eXBlIjogMCwidXNlcklkIjogMH1dLCJwYXlUeXBlIjogMSwicHJvbW90aW9uSWQiOiAwLCJwcm92aW5jZSI6ICLkuIrmtbfluIIiLCJyZW1hcmsiOiAi5b+r54K56YCB5p2lIiwicm9sZUlkIjogMCwic3RvcmVJZCI6IDAsInVzZXJJZCI6IDB9111111").toUpperCase());
    	System.out.println(MD5.passmd5("123456"));
    	
    	
    	System.out.println("---" + MD5.encode("data=eyJhbW91bnQiOiIyMDAiLCJvcmRlcklkIjoiMzI1NDM0NTQzNjQ1NjQ1IiwidXNlcklkIjoiNTI0MTIifQ==&key=5359fd245e3fe71c0dee8ed8788f0b9d"));
		
	}
    
}